package it.univaq.mancoosi.injectors.systemconfiguration.managers;

import it.univaq.mancoosi.injectors.systemconfiguration.managers.ubuntu.UbuntuEnvironmentManager;
import it.univaq.mancoosi.mancoosimm.File;
import it.univaq.mancoosi.mancoosimm.MancoosiFactory;
import it.univaq.mancoosi.mancoosimm.SGMLCatalog;
import it.univaq.mancoosi.mancoosimm.SGMLDocument;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

public abstract class SGMLCatalogManager {

	protected static SGMLCatalogManager INSTANCE;
	protected MancoosiFactory factory; 
	protected SGMLCatalog sgmlCatalog;
	protected HashMap<String, SGMLDocument> sgmlMap;

	protected SGMLCatalogManager(){
		factory = MancoosiFactory.eINSTANCE;
		sgmlCatalog = factory.createSGMLCatalog();
		this.sgmlMap = new HashMap<String, SGMLDocument>();
	}

	public static SGMLCatalogManager getInstance() {
		return INSTANCE;
	}

	public abstract void createSGMLCatalogFromSystem();

	public abstract Boolean exists(String sgmlDocumentName);

	public abstract void synchronize() throws IOException;

	public SGMLCatalog getSGMLCatalog() {
		return sgmlCatalog;
	}

	protected void syncMap () {
		Iterator<SGMLDocument> iterator  = null;
		if(UbuntuEnvironmentManager.getInstance().getEnvironment().getSgmlCatalog() != null){
			iterator = UbuntuEnvironmentManager.getInstance().getEnvironment().getSgmlCatalog().getDocuments().iterator();
		}
		try {
			if(iterator != null){
				while (iterator.hasNext()) {
					SGMLDocument curr = iterator.next();
					this.sgmlMap.put(curr.getName(), curr);
				}
			}
		}
		catch (NullPointerException e) {
			e.printStackTrace();
		}
	}
}
